package org.osomit.sacct.security.otp.impl;

import org.osomit.sacct.security.otp.iface.OTPTokenReader;
import org.osomit.sacct.security.otp.iface.OTPToken;
import org.osomit.sacct.security.utils.Serializer;
import org.osomit.sacct.security.utils.SymmetricCipher;
import org.apache.commons.codec.binary.Base64;

/**
 * Read the OTP Token from Spring XML wiring
 *
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 26, 2009
 */
public class XMLOTPTokenReader implements OTPTokenReader {
	private String tokenAsString;

	private String password;
	
	public final String getTokenAsString() {
		return tokenAsString;
	}

	public final String getPassword() {
		return password;
	}

	public final void setPassword(String password) {
		this.password = password;
	}

	public final void setTokenAsString(String tokenAsString) {
		this.tokenAsString = tokenAsString;
	}


	public OTPToken read() throws Exception {
		byte[] encrypted = Base64.decodeBase64(this.tokenAsString.getBytes());
		byte[] decrypted = SymmetricCipher.desDecrypt(this.password, encrypted);
		OTPToken token = (OTPToken) Serializer.decompressAndDeserialize(decrypted, false);

		return token;
	}

}
